package pers.hl.module_main.mine

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import pers.hl.lib_global.common.MMKVHelper
import pers.hl.lib_global.http.LibResponse
import pers.hl.lib_global.model.bean.*
import pers.hl.lib_global.mvvm.BaseLibModel
import java.io.File

class MineModel: BaseLibModel() {

    suspend fun borrowBook(bookId: Int): LibResponse<*> {
        return libService.borrow(bookId)
    }

    suspend fun fetchBookById(bookId: Int): LibResponse<Book> {
        return libService.fetchBookById(bookId)
    }

    suspend fun fetchMyBorrows(): LibResponse<List<BookBorRec>> {
        return libService.fetchMyBorrows()
    }

    suspend fun cancel(recordId: Int): LibResponse<*> {
        return libService.cancel(recordId)
    }

    suspend fun accept(recordId: Int): LibResponse<*> {
        return libService.accept(recordId)
    }

    suspend fun refuse(recordId: Int): LibResponse<*> {
        return libService.refuse(recordId)
    }

    suspend fun returnBack(recordId: Int): LibResponse<*> {
        return libService.returnBack(recordId)
    }

    suspend fun fetchSharing(userId: Int): LibResponse<List<BookShareRec>> {
        return libService.userShare(userId)
    }

    suspend fun fetchMyPublish(): LibResponse<List<BookShareRec>> {
        return libService.fetchUserPublish()
    }

    suspend fun collect(bookId: Int): LibResponse<*> {
        return libService.collect(bookId)
    }

    suspend fun unCollect(bookId: Int): LibResponse<*> {
        return libService.uncollect(bookId)
    }

    suspend fun fetchMyCollectBook(): LibResponse<List<Book>> {
        return libService.myCollectBook()
    }

    suspend fun fetchReplyList(cmtId: Int): LibResponse<List<Reply>> {
        return libService.fetchReplyListByCmtId(cmtId)
    }

    suspend fun deleteComment(cmtId: Int): LibResponse<*> {
        val ids = listOf(cmtId)
        return libService.deleteComment(ids)
    }

    suspend fun fetchMyComments(userId: Int): LibResponse<List<Comment>> {
        return libService.myComments(userId)
    }

    suspend fun fetchMyCollect(userId: Int): LibResponse<List<Collect>> {
        return libService.myCollect(userId)
    }

    suspend fun fetchBookByTag(tagId: Int): LibResponse<List<Book>> {
        return libService.getBooksByTag(tagId)
    }

    suspend fun fetchMyFollows(): LibResponse<List<User>> {
        return libService.userFollows(MMKVHelper.getUser()!!.userId)
    }

    suspend fun fetchMyFans(): LibResponse<List<User>> {
        return libService.userFans(MMKVHelper.getUser()!!.userId)
    }

    suspend fun fetchUserInfo(userId: Int): LibResponse<User> {
        return libService.userInfo(userId)
    }

    suspend fun isFollowed(followerId: Int, userId: Int): LibResponse<Boolean> {
        return libService.isFollowed(followerId, userId)
    }

    suspend fun follow(followRec: FollowRec): LibResponse<*> {
        return libService.follow(followRec)
    }

    suspend fun unFollow(followerId: Int, userId: Int): LibResponse<*> {
        return libService.unFollow(followerId, userId)
    }

    suspend fun scanBorrow(bookId: Int): LibResponse<*> {
        return libService.scanBorrow(bookId);
    }

    suspend fun updateUser(user: User): LibResponse<*> {
        return libService.updateUser(user)
    }

    suspend fun uploadAvatar(file: File, userId: Int, fileType: Int): LibResponse<FileEntity> {
        val requestFile = file.asRequestBody("multipart/form-data".toMediaTypeOrNull())
        val multipartBody = MultipartBody.Part.createFormData("file", file.name, requestFile)
        return libService.uploadFile(multipartBody, userId, fileType)
    }

    suspend fun checkExpiringRecord(): LibResponse<List<BookBorRec>> {
        return libService.checkExpiringRecord()
    }
}
